package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.common.exception.GuiguException;
import com.atguigu.spzx.manager.mapper.SysMenuMapper;
import com.atguigu.spzx.manager.mapper.SysRoleMenuMapper;
import com.atguigu.spzx.manager.service.SysMenuService;
import com.atguigu.spzx.manager.utils.MenuHelper;
import com.atguigu.spzx.model.entity.system.SysMenu;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.system.SysMenuVo;
import com.atguigu.spzx.utils.AuthContextUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;


@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Resource
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Resource
    private SysMenuMapper sysMenuMapper;

//    private final SysMenuMapper sysMenuMapper;
//
//    public SysMenuServiceImpl(SysMenuMapper sysMenuMapper) {
//        this.sysMenuMapper = sysMenuMapper;
//    }

    @Override
    public List<SysMenu> findNodes() {
        LambdaQueryWrapper<SysMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(SysMenu::getSortValue);
        List<SysMenu> sysMenuList = baseMapper.selectList(wrapper);
        if(CollectionUtils.isEmpty(sysMenuList)){
            return new ArrayList<>();
        }
        return MenuHelper.buildTree(sysMenuList);
    }

    @Override
    public void removeMenu(Long id) {
        LambdaQueryWrapper<SysMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysMenu::getParentId, id);
        Long count = baseMapper.selectCount(wrapper);
        if(count > 0){
            throw new GuiguException(ResultCodeEnum.HAS_CHILDREN_NODE);
        }
        baseMapper.deleteById(id);
    }

    @Override
    public List<SysMenuVo> findMenusByUserId() {
//        UserInfo userInfo = AuthContextUtil.getUserInfo();
        SysUser sysUser = AuthContextUtil.get();
        Long userId = sysUser.getId();

        List<SysMenu> sysMenuList = sysMenuMapper.findMenusByUserId(userId);

        List<SysMenu> sysMenusList = MenuHelper.buildTree(sysMenuList);

        List<SysMenuVo> sysMenuVos = this.buildMenus(sysMenusList);

        return sysMenuVos;
    }

    @Override
    public void saveMenu(SysMenu sysMenu) {
        sysMenuMapper.save(sysMenu);
        updateSysRoleMenu(sysMenu);
    }

    //新添加子菜单，把父菜单isHalf半开状态 1
    private void updateSysRoleMenu(SysMenu sysMenu) {
        //获取当前添加菜单的父菜单
        SysMenu parentMenu = sysMenuMapper.selectParentMenu(sysMenu.getParentId());
        if(parentMenu != null) {
            //把父菜单isHalf半开状态 1
            sysRoleMenuMapper.updateSysRoleMenuIsHalf(parentMenu.getId()) ;
            // 递归调用
            updateSysRoleMenu(parentMenu) ;
        }
    }

    // 将List<SysMenu>对象转换成List<SysMenuVo>对象
    private List<SysMenuVo> buildMenus(List<SysMenu> menus) {

        List<SysMenuVo> sysMenuVoList = new LinkedList<SysMenuVo>();
        for (SysMenu sysMenu : menus) {
            SysMenuVo sysMenuVo = new SysMenuVo();
            sysMenuVo.setTitle(sysMenu.getTitle());
            sysMenuVo.setName(sysMenu.getComponent());
            List<SysMenu> children = sysMenu.getChildren();
            if (!CollectionUtils.isEmpty(children)) {
                sysMenuVo.setChildren(buildMenus(children));
            }
            sysMenuVoList.add(sysMenuVo);
        }
        return sysMenuVoList;
    }
}
